package cn.iocoder.yudao.module.product.service.recommend;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.recommend.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.recommend.RecommendDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.recommend.RecommendMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 落地页SKU推荐 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class RecommendServiceImpl implements RecommendService {

    @Resource
    private RecommendMapper recommendMapper;

    @Override
    public Long createRecommend(RecommendSaveReqVO createReqVO) {
        // 插入
        RecommendDO recommend = BeanUtils.toBean(createReqVO, RecommendDO.class);
        recommendMapper.insert(recommend);
        // 返回
        return recommend.getId();
    }

    @Override
    public void updateRecommend(RecommendSaveReqVO updateReqVO) {
        // 校验存在
        validateRecommendExists(updateReqVO.getId());
        // 更新
        RecommendDO updateObj = BeanUtils.toBean(updateReqVO, RecommendDO.class);
        recommendMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecommend(Long id) {
        // 校验存在
        validateRecommendExists(id);
        // 删除
        recommendMapper.deleteById(id);
    }

    private void validateRecommendExists(Long id) {
        if (recommendMapper.selectById(id) == null) {
            throw exception(RECOMMEND_NOT_EXISTS);
        }
    }

    @Override
    public RecommendDO getRecommend(Long id) {
        return recommendMapper.selectById(id);
    }

    @Override
    public PageResult<RecommendDO> getRecommendPage(RecommendPageReqVO pageReqVO) {
        return recommendMapper.selectPage(pageReqVO);
    }

    public List<RecommendDO> listBySpuId(Long spuId) {
        return recommendMapper.selectList(RecommendDO::getSpuId, spuId);
    }

    public List<RecommendDO> listByCategoryId(Long categoryId) {
        return recommendMapper.selectList(RecommendDO::getCategoryId, categoryId);
    }

}
